package com.usdt.common.core.domain;

import java.io.Serializable;

import com.alibaba.fastjson2.JSONObject;
import com.usdt.common.constant.HttpStatus;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 公共Response
 *
 **/
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultBody<G> implements Serializable {
    /**
     * 响应Code
     */
    private Integer code;

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应结果
     */
    private G data;


    /**
     * 成功
     *
     * @return
     */
	public static <M> ResultBody<M> success() {
    	ResultBody<M> body = new ResultBody<>();
    	body.setCode(HttpStatus.SUCCESS);
        return body;
    }

    /**
     * 成功
     *
     * @param data
     * @return
     */
    public static <M> ResultBody<M> success(M data) {
        ResultBody<M> body = new ResultBody<>();
        body.setCode(HttpStatus.SUCCESS);
        body.setData(data);
        return body;
    }


    /**
     * 失败
     */
    public static <M> ResultBody<M> error(Integer code, String message) {
        ResultBody<M> rb = new ResultBody<>();
        rb.setCode(code);
        rb.setMsg(message);
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     *
     * @param code
     * @param message
     * @param result
     * @param <M>
     * @return
     */
    public static <M> ResultBody<M> error(Integer code, String message, M result) {
        ResultBody<M> rb = new ResultBody<>();
        rb.setCode(code);
        rb.setMsg(message);
        rb.setData(result);
        return rb;
    }

    /**
     * 失败
     */
    public static <M> ResultBody<M> error(String message) {
        ResultBody<M> rb = new ResultBody<>();
        rb.setCode(HttpStatus.ERROR);
        rb.setMsg(message);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

}
